
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import { formatNumber } from 'vant/lib/utils'

const routes = [
  {
    path: '/',
    redirect: '/login',
    name: 'App'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/my',
    name: 'My',
    component: () => import('@/views/My.vue')
  },
  {
    path: '/realTimeWeighing',
    name: 'realTimeWeighing',
    component: () => import('@/views/compontent/realTimeWeighing.vue')
  },
  {
    path: '/weighingRecord',
    name: 'weighingRecord',
    component: () => import('@/views/compontent/weighingRecord.vue')
  },
  {
    path: '/weightManagement',
    name: 'weightManagement',
    component: () => import('@/views/compontent/weightManagement.vue')
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('@/views/compontent/detail.vue')
  },
  {
    path: '/record',
    name: 'record',
    component: () => import('@/views/compontent/record.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 路由拦截  执行前置路由拦截 permissionController


router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token') != undefined || localStorage.getItem('token') != null ? true : false
  if (!token && to.path !== '/login') {
    next({ path: '/login' })
  } else {
    if (!to.name) {
      next({ path: from.path })
    } else {
      next()
    }
  }
})

export default router
